﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AntUnion.FrameWork.Web.Extend
{
    public static class ExternEnumerable
    {
        /// <summary>
        /// 根据字段过滤重复
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IQueryable<TSource> Paging<TSource>(this IQueryable<TSource> source, int page, int limit)
        {
            //if (page < 1)
            //    page = 1;
            //if (limit < 0 || limit > 20)
            //    limit = 20;
            return source.Skip((page - 1) * limit).Take(limit);
        }

        public class PageData<T>
        {
            public T Data { get; set; }
            public int Count { get; set; }
        }
    }
}
